package com.example.suduku;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.Toast;

public class SuduActivity extends AppCompatActivity {


    private GridLayout sudokuGrid;
    private Button btnGenerate, btnValidate,btnBack;
    private EditText[][] cells = new EditText[9][9];
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sudu);

        sudokuGrid = findViewById(R.id.sudoku_grid);
        btnGenerate = findViewById(R.id.btn_generate);
        btnValidate = findViewById(R.id.btn_validate);
        btnBack = findViewById( R.id.btn_back );
        // 获取从 MainActivity 页面传递过来的按钮 ID
        int id = getIntent().getIntExtra("button_id", -1); // 默认值 -1

        // 初始化网格
        initializeGrid();

        // 生成数独谜题
        btnGenerate.setOnClickListener(v -> generatePuzzle(id));

        // 验证答案
        btnValidate.setOnClickListener(v -> validatePuzzle());
        btnBack.setOnClickListener(this::onClick);
    }
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_back) {
            Intent i = new Intent( SuduActivity.this, MainActivity.class );
            startActivity( i );
        }
    }
//    private void initializeGrid() {
//        for (int row = 0; row < 9; row++) {
//            for (int col = 0; col < 9; col++) {
//                EditText cell = new EditText(this);
//                cell.setLayoutParams(new GridLayout.LayoutParams());
//                cell.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
//                cell.setMaxLines(1);
//                cell.setTextSize(20);
//                cells[row][col] = cell;
//                sudokuGrid.addView(cell);
//            }
//        }
//    }
private void initializeGrid() {
    sudokuGrid.setRowCount(9);
    sudokuGrid.setColumnCount(9);

    for (int row = 0; row < 9; row++) {
        for (int col = 0; col < 9; col++) {
            EditText cell = new EditText(this);
            cell.setLayoutParams(new GridLayout.LayoutParams(
                    GridLayout.spec(row, 1f),
                    GridLayout.spec(col, 1f) // 设置权重，使格子均匀分布
            ));
            cell.setGravity( Gravity.CENTER); // 文字居中
            cell.setInputType( InputType.TYPE_CLASS_NUMBER);
            cell.setMaxLines(1);
            cell.setTextSize(24);
            // 设置背景，实现格子线
            cell.setBackgroundResource(R.drawable.cell_border);

            cells[row][col] = cell;
            sudokuGrid.addView(cell);
        }
    }
}

    // 随机生成数独谜题
    private void generatePuzzle(int id) {
        SudokuGenerator generator = new SudokuGenerator();
        int[][] puzzle = generator.generate(id);

        // 显示谜题
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                EditText cell = cells[i][j];
                if (puzzle[i][j] != 0) {
                    cell.setText(String.valueOf(puzzle[i][j]));
                    cell.setEnabled(false); // 固定的数字不可编辑
                } else {
                    cell.setText("");
                    cell.setEnabled(true); // 可编辑的数字
                }
            }
        }
    }

    // 验证用户答案
    private void validatePuzzle() {
        boolean valid = true;
        int[][] userSolution = new int[9][9];

        // 获取用户输入
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                String input = cells[i][j].getText().toString();
                if (!input.isEmpty()) {
                    userSolution[i][j] = Integer.parseInt(input);
                }
            }
        }

        // 使用生成器来验证解决方案
        SudokuValidator validator = new SudokuValidator();
        valid = validator.isValid(userSolution);

        if (valid) {
            Toast.makeText(this, "答案正确！", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "答案错误！", Toast.LENGTH_SHORT).show();
        }
    }
}